//==============================================================================
// 21AT-WebGIS平台客户端程序，版权所有，二十一世纪空间技术应用股份有限公司，2000-2009。
// 作者：21AT-WebGIS平台开发组 
// 修改：	 
// 文件名：Measure.js  
// 功能：地图量算控件
// 最后修改时间：	2009-05-05
//==============================================================================

/**
 * Class: OpenLayers.Control.MeasureDistance
 * 
 * 地图距离量算控件
 * 
 * 
 * 继承自:
 *  - <OpenLayers.Control.Measure>
 */
/**
 * @requires OpenLayers/Control/Measure.js
 */
 OpenLayers.Control.MeasureDistance = OpenLayers.Class(OpenLayers.Control.Measure, {
    /**
     * Property: type
     * {String} 工具类型
     */
    type: OpenLayers.Control.TYPE_TOOL,
	
	/**
   * Property: outputDiv
   * {String} 量算结果输出DIV
   */
	outputDiv: null,
	
	/**
	   * Property: correctParam
	   * 纠正系数
	*/
	correctParam: 1,
	
	/**
   * Property: clicks
   * {Integer} 鼠标点击次数
   */
	clicks: null,
	
	/**
   * Property: title
   * {String} 工具按钮提示文字
   */
	title: "距离量算",
	
	/**
   * Constructor: OpenLayers.Control.MeasureDistance
   * 初始化函数
   * 
   * Parameters:
   * handler - {<OpenLayers.Handler>} 
   * options - {Object} 
   */
	initialize: function(options) {
		OpenLayers.Util.extend(this, options);
	   	var style = new OpenLayers.Style();
	   	style.addRules([
	       new OpenLayers.Rule({symbolizer: measureSymbolizers})]);
	       
	   	var styleMap = new OpenLayers.StyleMap({"default": style});
	   	test={layerOptions:{stypeMap:styleMap}};
	   	newArguments = [OpenLayers.Handler.Path, {
	       persist: true,
	       handlerOptions: {
	           layerOptions: {styleMap: styleMap}
	       }
	   	}];
		OpenLayers.Control.Measure.prototype.initialize.apply(this,newArguments);
		this.events.on({
		    measure: this.handleMeasurements,
		    measurepartial: this.handlePartialMeasurements
		});
		
		var modifyFeature = this.handler.modifyFeature;
		var scope = this;
		this.handler.modifyFeature = function(pixel, drawing){
		    modifyFeature.apply(scope.handler, arguments);
		    scope.map.layerContainerDiv.style.cursor = "url("+OpenLayers.Util.getRootPath() + "img/MeasureDistance.cur),default";
		}
		this.clicks = 0;
	},
		
	
	/**
	 * Method: activate
	 * 控件被触发时设置鼠标手势
	 */
	activate: function(){
		if(this.map.labelLayer){
			this.map.labelLayer.clear();
		}else{
			this.map.labelLayer = new OpenLayers.Layer.Labels("mesurelabel", "mesurelabelLayer");
			this.map.addLayer(this.map.labelLayer);
		}
		OpenLayers.Control.Measure.prototype.activate.apply(this, arguments);
		this.map.layerContainerDiv.style.cursor = "url("+OpenLayers.Util.getRootPath() + "img/MeasureDistance.cur),default";
	},
	
	/**
	 * Method: activate
	 * 控件被触发时设置鼠标手势
	 */
	deactivate: function(){
		OpenLayers.Control.Measure.prototype.deactivate.apply(this, arguments);
		if(this.map.labelLayer){
			this.map.labelLayer.clear();
		}
	},

	/**
	 * Method: handleMeasurements
	 * 量算结束
	 */
     handleMeasurements:function(event) {
        var geometry = event.geometry;
        var units = event.units;
        var order = event.order;
        var measure = event.measure;
        var out = "";
        var point="";
        if(order == 1) {
            out += "" + measure.toFixed(3)*this.correctParam + " " + units;
            var length=geometry.components.length;
            point =geometry.components[length-1];
        } else {
            out += "" + measure.toFixed(3)*this.correctParam + " " + units + "<sup>2</" + "sup>";
            var length=geometry.components[0].components.length;
            point =geometry.components[0].components[length-2];
        }
        this.map.labelLayer.setZIndex(this.map.Z_INDEX_BASE["Control"]);
        var labelDiv = new OpenLayers.Label(new OpenLayers.LonLat(point.x,point.y),out, "MeasureLable");
        var w = labelDiv.labelDiv.offsetWidth;
        //labelDiv.offset=new OpenLayers.Pixel(0,0);
        if(this.mesureend){
	        this.mesureend(measure.toFixed(3)*this.correctParam, units, geometry);
        }
        this.map.labelLayer.add(labelDiv);
    },
    
    /**
    * Method: measurePartial
    * Called each time a new point is added to the measurement sketch.
    *
    * Parameters:
    * point - {<OpenLayers.Geometry.Point>} The last point added.
    * geometry - {<OpenLayers.Geometry>} The sketch geometry.
    */
   measurePartial: function(point, geometry) {
        this.map.labelLayer.clear();
       if (geometry.getLength() > 0) {
           geometry = geometry.clone();
           this.delayedTrigger = window.setTimeout(
               OpenLayers.Function.bind(function() {
                   this.measure(geometry, "measurepartial");
               }, this),
               this.partialDelay
           );
       }
   },
    
	CLASS_NAME: "OpenLayers.Control.MeasureDistance"
});


/**
 * Class: OpenLayers.Control.MeasureArea
 * 
 * 地图面积量算控件
 * 
 * 
 * 继承自:
 *  - <OpenLayers.Control.MeasureDistance>
 */ 
OpenLayers.Control.MeasureArea = OpenLayers.Class(OpenLayers.Control.MeasureDistance, {
	
	/**
   * Property: title
   * {String} 工具按钮提示文字
   */
	title: "面积量算",
	
	
	
	/**
   * Constructor: OpenLayers.Control.MeasureDistance
   * 
   * Parameters:
   * handler - {<OpenLayers.Handler>} 
   * options - {Object} 
   */
	initialize: function(options) {
	    OpenLayers.Util.extend(this, options);
	   var style = new OpenLayers.Style();
       style.addRules([
           new OpenLayers.Rule({symbolizer: measureSymbolizers})]);
       var styleMap = new OpenLayers.StyleMap({"default": style});
       test={layerOptions:{stypeMap:styleMap}};
       newArguments = [OpenLayers.Handler.Polygon, {
           persist: true,
           handlerOptions: {
               layerOptions: {styleMap: styleMap}
           }
       }];
		OpenLayers.Control.Measure.prototype.initialize.apply(this,newArguments);
		this.events.on({
        measure: this.handleMeasurements,
        measurepartial: this.handlePartialMeasurements
    });
        var modifyFeature = this.handler.modifyFeature;
		var scope = this;
		this.handler.modifyFeature = function(pixel, drawing){
		    modifyFeature.apply(scope.handler, arguments);
		    scope.map.layerContainerDiv.style.cursor = "url("+OpenLayers.Util.getRootPath() + "img/MeasureArea.cur),default";
		}
		this.clicks = 0;
		
	},
	
	/**
	 * Method: activate
	 * 控件被触发时设置鼠标手势
	 */
	activate: function(){
		if(this.map.labelLayer){
			this.map.labelLayer.clear();
		}else{
			this.map.labelLayer = new OpenLayers.Layer.Labels("mesurelabel", "mesurelabelLayer");
			this.map.addLayer(this.map.labelLayer);
		}
		OpenLayers.Control.Measure.prototype.activate.apply(this, arguments);
		this.map.layerContainerDiv.style.cursor = "url("+OpenLayers.Util.getRootPath() + "img/MeasureArea.cur),default";
	},
	
	CLASS_NAME: "OpenLayers.Control.MeasureArea"
});

//style the sketch fancy
var measureSymbolizers = {
    "Point": {
        pointRadius: 3,
        graphicName: "square",
        fillColor: "#8AC4F0",
        fillOpacity: 1,
        strokeWidth: 1,
        strokeOpacity: 1,
        strokeColor: "#FAD00B"
    },
    "Line": {
        strokeWidth: 3,
        strokeOpacity: 1,
        strokeColor: "#ee9900",
        strokeDashstyle: "solid"
    },
    "Polygon": {
        strokeWidth: 2,
        strokeOpacity: 1,
        strokeColor: "#ee9900",
        fillColor: "#66cccc",
        fillOpacity: 0.3
    }
};

